package com.starsea.book.ui.newpage

import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.starsea.book.R
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityReadHistoryBinding
import com.starsea.book.ui.book.read.ReadBookActivity
import com.starsea.book.ui.newpage.adapter.ReadHistoryAdapter
import com.starsea.book.ui.newpage.entity.ReadHistoryEntity
import com.starsea.book.ui.newpage.viewmodel.MsgViewModel
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding
import com.starsea.book.utils.visible

/**
 * @author Lee
 * @createTime 2024-09-01
 * @desc : 浏览历史
 */
class ReadHistoryActivity : VMBaseActivity<ActivityReadHistoryBinding, MsgViewModel>() {
    override val binding by viewBinding(ActivityReadHistoryBinding::inflate)
    override val viewModel by viewModels<MsgViewModel>()

    var page = 1

    lateinit var mAdapter: ReadHistoryAdapter

    var mPosition: Int = 0

    override
    fun onActivityCreated(savedInstanceState: Bundle?) {

        mAdapter = ReadHistoryAdapter(this) { it, type, position ->
            if (type == 1) {
                if (it.shelfStatus == 1) {
                    openBook(it)
                } else {
                    mPosition = position
                    viewModel.addToShelf(it.bookId)
                }
            } else if (type == 2) {
                mPosition = position
                viewModel.deleteReadHis(it.recordId)
            } else {
                openBook(it)
            }
        }

        viewModel.addSherfResult.observe(this) {
            if (it.status == 200) {
                toastOnUi("加入书架成功")
                mAdapter.getItems()[mPosition].shelfStatus = 1
                mAdapter.notifyItemChanged(mPosition)
            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@ReadHistoryActivity)
            } else {
                toastOnUi(it.msg)
            }
        }

        binding.rvContent.layoutManager = LinearLayoutManager(this)
        binding.rvContent.adapter = mAdapter

        binding.refreshLayout.setOnRefreshListener {
            page = 1
            viewModel.getReadHistory(page)
        }
        binding.refreshLayout.setOnLoadMoreListener {
            page++
            viewModel.getReadHistory(page)
        }

        viewModel.getReadHistory(page)

        viewModel.deleteReadHistoryResult.observe(this) {
            if (it.status == 200) {

                mAdapter.removeItem(mPosition)

                binding.tvEmptyMsg.visible(mAdapter.isEmpty())

            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@ReadHistoryActivity)
            } else {
                toastOnUi(it.msg)
            }
        }

        viewModel.readHistoryResult.observe(this) {
            if (it.status == 200) {

                if (page == 1) {
                    binding.refreshLayout.finishRefresh()
                    mAdapter.setItems(it.data!!.list)
                } else {
                    binding.refreshLayout.finishLoadMore()
                    mAdapter.addItems(it.data!!.list ?: arrayListOf())
                }

                if (it.data!!.list.size < 10) {
                    binding.refreshLayout.finishLoadMoreWithNoMoreData()
                }

                binding.tvEmptyMsg.visible(mAdapter.isEmpty())

            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@ReadHistoryActivity)
            } else {
                toastOnUi(it.msg)
            }
        }


        viewModel.bookInfoResult.observe(this) {
            if (it.status == 200) {
                viewModel.saveBook(viewModel.bookData.value) {
                    startActivity<ReadBookActivity> {
                        putExtra("bookUrl", it.data?.bookId)
                        putExtra("inBookshelf", it.data?.bookShelfFlag == 1)
                    }
                }
            } else if (it.status == 401) {
                toastOnUi("登录过期，请重新登录")
                LoginActivity.start(this)
            } else {
                toastOnUi(it.msg)
            }

        }


    }

    private fun openBook(it: ReadHistoryEntity.List) {

        viewModel.getBookInfo(it.bookId)

    }



}